package com.itheima.test1;

import java.util.ArrayList;
import java.util.Collections;

public class MyThread extends Thread{

    //创建集合
    ArrayList<Integer> list;
    //构造方法
    public MyThread(ArrayList<Integer> list) {
        this.list = list;
    }
    @Override
    public void run() {
        //循环
        //同步代码块
        //判断已经达到了末尾
        //判断没有达到末尾
        while (true){
            synchronized (MyThread.class){
                if(list.size() == 0){
                    break;
                }else{
                    //进行抽奖
                    /*
                    方法1：进行打乱，然后获取0索引的值
                    方法2：利用Random进行抽取
                     */
                    Collections.shuffle(list);
                    Integer result = list.get(0);
                    list.remove(result);
                    System.out.println(getName() + "抽取到了" + result +"元大奖");
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
